package com.example.demo.service.shop;

import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: ProductServiceImpl
 * Author:   longzhonghua
 * Date:     2019/4/12 19:57
 *
 * @Description: $description$
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductRepository productRepository;
    @Override
    public List<Product> getProductList() {
        return productRepository.findAll();
    }

    @Override
    public void addSeckill(Long productId, int stock, int price, Long startTime, Long endTime) {
        Product product = productRepository.findByid(productId);
        product.setSeckill_start_time(startTime);
        product.setSeckill_end_time(endTime);
        product.setAvailable_count(product.getStockCount().intValue()-stock);
        product.setSeckill_count(stock);
        product.setSeckill_price(new Float(price));
        productRepository.save(product);
    }

    @Override
    public Product findByid(Long productId) {
        return productRepository.findByid(productId);
    }

    @Override
    public int updateStock(Long product_id, Integer count) {
        return productRepository.updateStock(product_id,count);
    }

    @Override
    public Page<Product> list(Integer page, Integer size) {
        return productRepository.findAll(PageRequest.of(page,size));
    }
}
